package org.example.volunteerend.controller;

import java.util.Map;

import org.example.volunteerend.dto.PageResponse;
import org.example.volunteerend.dto.Result;
import org.example.volunteerend.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api")
public class ImageController {

    private final ImageService imageService;
    
    @Autowired
    public ImageController(ImageService imageService) {
        this.imageService = imageService;
    }
    
    /**
     * 上传图片
     */
    @PostMapping("/images/upload")
    public Result<Map<String, Object>> uploadImage(
            @RequestParam("image") MultipartFile image,
            HttpServletRequest httpRequest,
            @RequestParam("image_type") String imageType) {
        Integer userId = (Integer) httpRequest.getAttribute("userId");
        if (userId == null) {
            return Result.error("未登录或登录已过期");
        }
        
        try {
            // 上传图片
            Map<String, Object> data = imageService.uploadImage(image, userId, imageType);
            
            return Result.success("图片上传成功", data);
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("图片上传失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除图片
     */
    @DeleteMapping("/images/{imageId}")
    public Result<Object> deleteImage(
            @PathVariable Integer imageId,
            HttpServletRequest httpRequest) {
        
        // 从JWT获取用户ID（用于权限验证）
        Integer userId = (Integer) httpRequest.getAttribute("userId");
        if (userId == null) {
            return Result.error("未登录或登录已过期");
        }
        
        try {
            // 删除图片
            boolean success = imageService.deleteImage(imageId);
            
            if (success) {
                return Result.success("图片删除成功", null);
            } else {
                return Result.error("图片删除失败");
            }
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("图片删除失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取用户图片列表
     */
    @GetMapping("/users/images")
    public Result<Map<String, Object>> getUserImages(
           HttpServletRequest httpRequest,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String image_type) {
        // 从JWT获取用户ID（用于权限验证）
        Integer userId = (Integer) httpRequest.getAttribute("userId");
        if (userId == null) {
            return Result.error("未登录或登录已过期");
        }
        try {
            // 获取用户图片列表
            PageResponse<Map<String, Object>> pageResponse = imageService.getUserImages(userId, image_type, page, pageSize);
            
            // 构建返回结果
            Map<String, Object> data = Map.of(
                "total", pageResponse.getTotal(),
                "rows", pageResponse.getRecords()
            );
            
            return Result.success("success", data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取图片列表失败：" + e.getMessage());
        }
    }
} 